import os
import typing
import csv
from sai import Pipeline, Item


class CsvPipeline(Pipeline):
    '''Buildin CsvPipeline
    check csv_pipeline_usage_login2'''
    def __init__(self, fp: str=None):
        self.fp = fp
        self.csv_file = None
        self.csv_writer = None
        self.write_head = True

    def open_spider(self, spider):
        downloads_dir = './downloads'
        if not os.path.exists(downloads_dir):
            os.makedirs(downloads_dir)
        if self.fp is None:
            self.fp = f"{downloads_dir}/{spider.name}.csv"

        self.csv_file = open(self.fp, 'a+', newline='', encoding='utf-8')
        # quoting default value is  csv.QUOTE_MINIMAL
        self.csv_writer = csv.writer(self.csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)

    def process_item(self, item: typing.Union[Item, dict], spider):
        if(self.write_head):
            csv_head = list(item.keys())
            self.csv_writer.writerow(csv_head)
            self.write_head = False
        self.csv_writer.writerow(list(item.values()))

    def close_spider(self, spider):
        self.csv_file.close()
